{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#51CTO课程频道：http://edu.51cto.com/lecturer/index/user_id-12330098.html\n",
    "#优酷频道：http://i.youku.com/sdxxqbf\n",
    "#微信公众号：深度学习与神经网络\n",
    "#Github：https://github.com/Qinbf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-2 -1]\n",
      "[-1  1]\n"
     ]
    }
   ],
   "source": [
    "#定义一个变量\n",
    "x = tf.Variable([1,2])\n",
    "#定义一个常量\n",
    "a = tf.constant([3,3])\n",
    "#增加一个减法op\n",
    "sub = tf.subtract(x,a)\n",
    "#增加一个加法op\n",
    "add = tf.add(x,sub)\n",
    "\n",
    "#所有变量初始化\n",
    "init = tf.global_variables_initializer()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    #执行变量初始化\n",
    "    sess.run(init)\n",
    "    print(sess.run(sub))\n",
    "    print(sess.run(add))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "#创建一个变量初始化为0\n",
    "state = tf.Variable(0,name='counter')\n",
    "#创建一个op，作用是使state加1\n",
    "new_value = tf.add(state,1)\n",
    "#赋值op\n",
    "update = tf.assign(state,new_value)\n",
    "#所有变量初始化\n",
    "init = tf.global_variables_initializer()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    #执行变量初始化\n",
    "    sess.run(init)\n",
    "    print(sess.run(state))\n",
    "    for _ in range(5):\n",
    "        sess.run(update)\n",
    "        print(sess.run(state))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
